Installing Go on Local Machine
Learn to set up Go on local machines.
Technical requirements#
The technical requirements for this section are as follows:
A computer with an OS supported by the Go tools.
An internet connection and web browser to download the Go tools.
The Go compiler and toolset can be found here. Here, we’ll find releases for the macOS, Windows, and Linux platforms for a multitude of computing platforms.
The most common platform is the AMD64 architecture, which should be used for any x86 system. For macOS, it is important to note that if we are using a machine with a non-Intel-based CPU, such as an Apple M1, we’ll need to use the arm64 builds.
In the next sections, we'll describe methods of installing Go for the major OSs. We should skip to the OS we plan to install on.
macOS installation using the package installer#
The easiest way to install the Go tooling for macOS is to use a .pkg installer. The download page offers .tar.gz builds and .pkg. With the tarballs, we must unpack the files in a location and add that location to our path. It also means we'll have to handle upgrades manually. We should only do this if we have advanced needs.
The .pkg file makes installation and upgrading simple. Simply double-click the .pkg file and follow the onscreen prompts to install. This may require entering in our credentials at a prompt.
Once installation is finished, open the Applications/Utilities/terminal.app terminal and type go version:
Note: The version output will depend on the version of Go that we have downloaded and the platform we are running on.
macOS installation via Homebrew#
Many developers on macOS prefer to use the popular Homebrew to install Go. If you're a Homebrew user, there is a simple two-step process for installing Go, as explained in the following sections.
Installing Xcode#
Go has some reliance on Apple's Xcode, and it needs to be installed in order to work correctly. To see whether we have Xcode installed, type the following:
This should output something like this:
If it gives an error, we need to install Xcode by following this link on the App Store. Once installed, we can install the separate command-line tools with the following:
Now, let's look at the next step.
Homebrew update and Go installation#
Update Homebrew and install the latest Go tools with the following:
We can verify the Go version with go version. Next, we'll look at installation on Windows.
Windows installation using MSI#
Windows installation is similar to other Windows application installations using a Microsoft Installer (MSI) file. Simply download the MSI file and follow the onscreen instructions. By default, this will install the Go tooling at Program Files or Program Files (x86).
To verify that Go was installed correctly, click the “Start” menu, type “cmd” into the search box, and the Command Prompt shell should appear. Type go version, and it should display the installed version of Go.
Next, we'll look at installation on Linux.
Linux#
Linux package management could be the subject of its own series of books and, as Linus points out, it is one of the reasons why Linux as a desktop system has failed so spectacularly. If we are using Linux for development, chances are we have some knowledge on how to install packages for our distribution. As we can't cover all possible methods of installation on Linux, we are going to cover installation using apt, Snap, and via tarball.
Linux installation via APT on Ubuntu#
APT is a package installation manager used in various distributions. Installing Go via APT is pretty straightforward. Update and upgrade APT to the latest version as follows:
Install the Go package as follows:
Now, type go version into the terminal, and it should display the installed version of Go.
Linux installation via Snap on Ubuntu#
Snap is a universal package manager meant to make the installation of a package easy across multiple distributions or versions by including all the necessary files in the package.
If we have Snap installed, we can simply use snap info go to locate a version of Go to install:
We can choose to install the latest stable version of Go by typing the following:
Now, type go version into the terminal, and it should display the installed version of Go.
Note that we may receive a warning about the Go package being built on a revision of Snap with classic confinement. In that case, to install using Snap, we may need to append - -classic as follows:
Linux installation via tarball#
In order to do this, we need to download the package for Linux and our platform. Our example will use go1.16.5.linux-amd64.tar.gz. We'll notice that the name gives the Go version (1.16.5), the OS (Linux), and the architecture (AMD64). We'll need to download the current version of Go and our architecture into a directory.
The rest of these instructions will use the terminal.
We want to install our version into /usr/local/go and remove any previous installations. This can be achieved with the following:
Now, let's add our directory to our PATH so that we can find our Go tools. This can be accomplished with the following:
With most shells, this change will not happen immediately. The easiest way to cause PATH to update is simply to open a new shell. We may also use the source command to reload our shell's profile if we know the name/location of our shell's profile – source $HOME/.profile, for example.
To test if our PATH was updated correctly, type go version, which should yield the following:
What about installing Go on other platforms?
Other platforms#
Go can certainly be installed on other platforms, such as FreeBSD, but those are not covered here. See the Go installation documentation for these other platforms.
A note on Go compiler version compatibility#
The Go project is governed by the Go compatibility promise. The gist is that Go will be backward compatible unless there is a major semantic version number change (1.x.x to 2.x.x). While we might hear people talking about Go 2.0, the authors have been very clear that they have no plans to leave version 1.
This means the software written for Go 1.0.0 works in the latest Go 1.17.5 version. This has been a major win for the Go community in stability. This course will be using Go 1.17.5 for its revision.
By the end of this lesson, we should have installed the Go tooling and tested that the tooling is working for our OS of choice. In the next section, we'll discuss how to build code on our machine.
Summary and Quiz on Designing for Chaos
Building Code Locally